package com.sup.generator.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sup.generator.beans.GrUserBean;
import com.sup.generator.beans.GrRegisterGrUserBean;
import com.sup.generator.config.GrRedisPrefixConfig;
import com.sup.generator.entity.GrUserEntity;
import com.sup.generator.exception.GrException;
import com.sup.generator.fixedvalue.GrConst;
import com.sup.generator.http.GrRequestData;
import com.sup.generator.http.GrResponseData;
import com.sup.generator.fixedvalue.GrMessage;
import com.sup.generator.service.GrUserService;
import com.sup.generator.utils.GrBase64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zh
 * @version v1.0
 * @description:
 * @date 2020/11/13 11:33
 */
@Slf4j
@RestController
@RequestMapping(GrConst.API_PREFIX)
public class GrUserController {

    @Autowired
    private GrUserService userService;

    /**
     * 登录接口
     * @param request
     * @return
     */
    @PostMapping("login")
    public GrResponseData login(@RequestBody GrRequestData<JSONObject> request) {
        JSONObject userBean = request.getParams();
        String name = userBean.getString("name");
        String password = userBean.getString("password");
        try{
            GrUserBean grUserBean = userService.login(name, password);
            String token = GrBase64Util.base64Encryption(String.format("%s%s",grUserBean.getGrUserEntity().getUId(),GrRedisPrefixConfig.LOGIN_TOKEN));
            return GrResponseData.success(grUserBean, GrMessage.LOGIN_SUCCESS,token);
        }catch (Exception e){
            log.error("{}:{}",GrMessage.LOGIN_FAIL.getMessage(),e.getMessage());
            throw new GrException(GrMessage.LOGIN_FAIL);
        }
    }

    @GetMapping("get-id")
    public GrResponseData login(@RequestParam("id") Long uId) {
        try{
            GrUserEntity userEntity = userService.getById(uId);
            return GrResponseData.success(userEntity, GrMessage.LOGIN_SUCCESS);
        }catch (Exception e){
            log.error("{}:{}",GrMessage.SELECT_FAIL.getMessage(),e.getMessage());
            throw new GrException(GrMessage.SELECT_FAIL);
        }
    }

    /**
     * 注册接口
     * @param grRequestData
     * @return
     */
    @PostMapping("register")
    public GrResponseData register(@RequestBody GrRequestData<GrRegisterGrUserBean> grRequestData){
        GrRegisterGrUserBean grUserBean = grRequestData.getParams();
        GrUserEntity grUserEntity = JSON.parseObject(JSON.toJSONString(grUserBean),GrUserEntity.class);
        try {
            GrUserEntity resultGrUserEntity = userService.register(grUserEntity);
            return GrResponseData.success(resultGrUserEntity,GrMessage.REGISTER_SUCCESS);
        }catch (Exception e){
            log.error("{}:{}",GrMessage.REGISTER_FAIL,e.getMessage());
            throw new GrException(GrMessage.REGISTER_FAIL);
        }
    }


    /**
     * 退出接口
     * @return
     */
    @GetMapping("login-out")
    public GrResponseData loginOut(GrUserEntity userEntity){
        try {
            Boolean loginOut = userService.loginOut(userEntity.getUId());
            if(loginOut){
                return GrResponseData.success(200,GrMessage.EXECUTE_SUCCESS);
            }
            return GrResponseData.success(500,GrMessage.EXECUTE_FAIL);
        }catch (Exception e){
            log.error("{}",e.getMessage());
            throw new GrException(e.getMessage());
        }
    }

}
